Ext.define('AmpleHarvest.objects.groups.eventHandler.cWindowEvents',
{
	extend						: 'Ext.AbstractComponent',
	alias						: 'widget.ampleharvest-objects-groups-eventhandler-cwindowevents',
	requires					:
	[
		"Ext.window.MessageBox"
	],
	uses						: [
	// 'AmpleHarvest.harvestRequest.window.request.cWindow',
	// 'AmpleHarvest.harvestRequest.window.deactivation.cWindow',
	// 'AmpleHarvest.search.window.cSave_EditQuery'
	],
	m_obj_Windows				: null,
	m_obj_MsgBus				: null,
	m_obj_Desktop				: null,
	statics						:
	{
		C_STR_EVENT_GROUP_CHANGE			: "dogroupchange",
		C_STR_EVENT_GROUP_SET_APPROVAL		: "dogroupsetapproval",
		C_STR_EVENT_GROUP_SET_EDIT			: "dogroupsetedit",
		C_STR_EVENT_GROUP_IS_ADMIN			: "dogroupisadmin",
		C_STR_EVENT_GROUP_IS_MEMBER			: "dogroupismember",
		C_STR_EVENT_GO_GROUP_ADD_MEMBER		: "dogogroupaddmember",
		C_STR_EVENT_GO_GROUP_REMOVE			: "dogogroupremovemember",
		C_STR_EVENT_MEMBER_STATUS			: "dogroupmemberstatus",
		C_STR_EVENT_GROUP_OPEN				: "dogroupopen",
		C_STR_EVENT_DETAILS_LOAD			: "dodetailsload"
		
	},

	initComponent				: function()
	{
		var obj_This = this;

		this.m_obj_MsgBus.addEvents(this.self.C_STR_EVENT_GROUP_CHANGE,
				this.self.C_STR_EVENT_GROUP_SET_APPROVAL,
				this.self.C_STR_EVENT_GO_GROUP_ADD_MEMBER,
				this.self.C_STR_EVENT_GO_GROUP_REMOVE,
				this.self.C_STR_EVENT_MEMBER_STATUS,
				this.self.C_STR_EVENT_GROUP_OPEN,
				this.self.C_STR_EVENT_DETAILS_LOAD
				
		);

		this.m_obj_MsgBus.on(this.self.C_STR_EVENT_GROUP_CHANGE, function()
				{
					alert("refresh")
				}, this);
		// Groups

		this.m_obj_MsgBus.on(this.self.C_STR_EVENT_GROUP_SET_APPROVAL,
				this.do_GroupSetApproval, this
		);
		
		this.m_obj_MsgBus.on(this.self.C_STR_EVENT_GROUP_SET_EDIT,
				this.do_GroupSetEdit, this
		);
		
		this.m_obj_MsgBus.on(this.self.C_STR_EVENT_GROUP_IS_ADMIN,
				this.do_GroupIsAdmin, this
		);
		
		this.m_obj_MsgBus.on(this.self.C_STR_EVENT_GROUP_IS_MEMBER,
				this.do_GroupIsMember, this
		);
		
		this.m_obj_MsgBus.on(this.self.C_STR_EVENT_GO_GROUP_ADD_MEMBER,
				this.do_GoGroupAddMember, this
		);
		
		this.m_obj_MsgBus.on(this.self.C_STR_EVENT_GO_GROUP_REMOVE,
				this.do_GoGroupRemoveMember, this
		);
		
		this.m_obj_MsgBus.on(this.self.C_STR_EVENT_MEMBER_STATUS,
				this.do_GroupMemberStatus, this
		);
		
		this.m_obj_MsgBus.on(this.self.C_STR_EVENT_GROUP_OPEN,
				this.do_OpenGroup, this
		);
		
		this.m_obj_MsgBus.on(this.self.C_STR_EVENT_DETAILS_LOAD,
				this.do_DetailsLoad, this
		);
		
		obj_This.callParent();
	},

	do_GroupApproval			: function(obj_Group)
	{
		this.m_obj_GroupData = obj_Group;
		Ext.Msg.confirm("Approve Group", "Are you sure you wish to approve "
						+ obj_Group.name, this.approval_Confirmation, this);
	},
	
	approval_Confirmation		: function(str_Return)
	{
		var obj_Params = new Object();
		if (str_Return == "yes")
		{
			obj_Params.id = this.m_obj_GroupData.id;
			obj_Params.approve = true;

			this.do_RestPost(obj_Params);
		}
		else
		{
			alert("Cancle Approval Action");
			return;
		}

	},	
	
	do_GroupSetEdit				: function(obj_Group)
	{		
		location = m_obj_AmpleHarvest_ViewPort.configObject.C_STR_AMPLEHARVEST_BASE_PAGES_PATH+"/groups/editGroup.php/?id="
				+ obj_Group.id;
	},

	do_GroupDisapproval			: function(obj_Group)
	{
		this.m_obj_GroupData = obj_Group;
		Ext.Msg.confirm("Disapprove Group",
				"Are you sure you wish to disapprove " + obj_Group.name,
				this.disapproval_Confirmation, this
		);
	},

	disapproval_Confirmation	: function(str_Return)
	{
		if (str_Return == "yes")
		{
			Ext.Msg
					.prompt(
							"Input Needed",
							"Please enter the reason you are dissapproveing this group request.",
							function(str_Response, str_Input, obj_Prompt)
							{
								var obj_Params = new Object();
								if (str_Response == "ok")
								{
									obj_Params.id = this.m_obj_GroupData.id;
									obj_Params.disapprove = true;
									obj_Params.disapproveReason = str_Input;
									this.do_RestPost(obj_Params);
								}

							}, this, 5
					);
		}
		else
		{
			alert("Cancle Disapproval Action");
			return;
		}
	},

	do_RestPost					: function(obj_Params)
	{
		Ext.Ajax.request(
		{
			url		: m_obj_AmpleHarvest_ViewPort.configObject.C_STR_EXTERNAL_PATH+'/rest/group/group.php',
			waitMsg	: 'Updateing Group Request',
			method	: "post",
			success	: function(obj_Response, mix_Arg)
			{
				var obj_Return = Ext.decode(obj_Response.responseText);

				if (obj_Return.success)
				{
					Ext.MessageBox.alert("Success", "Group Update Successful",
							close
					);
					this.m_obj_MsgBus
							.fireEvent(this.self.C_STR_EVENT_GROUP_CHANGE);
				}
				else
				{
					Ext.MessageBox.alert("Error", obj_Return.errors.msg, close);
				}
			},
			failure	: function(obj_Response, mix_Arg)
			{
				Ext.MessageBox.alert("Error", mix_Arg.result.errors.msg, close);
			},
			scope	: this,
			params	: obj_Params
		}
		);
	},

	do_GroupSetApproval			: function(obj_Group, bool_Approved)
	{

		if (bool_Approved)
		{
			this.do_GroupApproval(obj_Group);
		}
		else
		{
			this.do_GroupDisapproval(obj_Group);
		}
	},
	
	do_GoGroupAddMember		: function(obj_Panel)
	{
		var obj_Window = Ext.create('AmpleHarvest.objects.groups.window.members.cAddMember',
				{
					obj_member_panel : obj_Panel
				});
		this.m_obj_Desktop.add(obj_Window);
		obj_Window.show();
	},
	
	do_GroupIsAdmin			: function(obj_Group, str_userID, str_groupID)
	{
		if(true)
		{
			return true;
		}
		return false;
	},
	
	do_GoGroupRemoveMember	: function(obj_Panel, obj_tbar, str_userID, str_groupID)
	{
		Ext.Ajax.request({
		    url: m_obj_AmpleHarvest_ViewPort.configObject.C_STR_EXTERNAL_PATH+'/rest/group/member.php',
		    method	: "delete",
		    params: {
		    	userId		: str_userID,
		        groupId	: str_groupID
		    },
		    success: function(response){
		    	obj_tbar.set_Buttons([]);
		    	obj_Panel.store.load();
		    }
		});
	},
	
	do_GroupMemberStatus	: function(obj_Panel, obj_tbar, str_userID, str_groupID)
	{
		Ext.Ajax.request({
		    url: m_obj_AmpleHarvest_ViewPort.configObject.C_STR_EXTERNAL_PATH+'/rest/security/group.php',
		    method	: "post",
		    params: {
		        id		: str_userID,
		        group	: str_groupID
		    },
		    success: function(response){
		    	obj_tbar.set_Buttons([]);
		    	obj_Panel.store.load();
		    }
		});
	},

	do_OpenGroup				: function(str_ID)
	{
		m_obj_GrpoupManagerWin = Ext.create(
		"AmpleHarvest.objects.groups.cGroupManager",
		{
			m_str_userId	: "",
			m_str_groupId	: str_ID
		});
		this.m_obj_MsgBus.replace_content(m_obj_GrpoupManagerWin);
	},
	
	do_DetailsLoad				: function(obj_details)
	{
		var detailPanel = this.m_obj_MsgBus.down('ampleharvest-objects-groups-window-cdetailview');
		
		detailPanel.bookTpl.overwrite(detailPanel.body, obj_details);
	}
}
);